/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-09-01 09:15:51
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-06-05 16:26:18
 * @FilePath: \cxl-admin\src\pages\consumeManage\consume\list\index.ts
 */
import { BaseData, BaseMethod } from '@/utils/basc-data'
import * as tableConfig from './config.json'
import { ref } from 'vue'
import { consumeList, consumeParamsInterface } from '@/apis/consume/consume'
import { CreateXMLHttpRequest } from '@/utils'

export default class ConsumeList extends BaseData implements BaseMethod {
    //选择的时间段
    date: string[] = []
    reportUrl = ref('')
    companyId = ''
    productType = 0
    constructor() {
        super(tableConfig)
        //区分超级管理员和本企业账号 与 其它账号
        if (this.uStore.isSuperAdmin || Number(this.uStore.companyId) === 1) {
            this.pageData.search.unshift(['公司名称/姓名', '请输入公司名称/姓名'])
        } else {
            this.companyId = this.uStore.companyId
        }

        this.pageData.search.unshift(
            ...[
                {
                    key: 'date',
                    type: 'daterange',
                    name: '',
                    modelValue: this.fromTo,
                    placeholder: '请选择时间',
                    change: (res: any) => {
                        this.date = res
                    }
                }
            ]
        )

        this.date.length = 0
        this.date.push(...this.fromTo)
        this.getData()
    }

    getData = (arg?: any) => {
        console.log(this.date)
        const obj: consumeParamsInterface = {
            current: this.pageData.pagination.pageNo,
            size: this.pageData.pagination.pageSize,
            queryDateStart: this.date.length ? this.date[0] : '',
            queryDateEnd: this.date.length ? this.date[1] : '',
            companyId: this.companyId
        }
        if (Number(this.productType)) {
            obj.productType = this.productType
        }

        consumeList(obj).then(res => {
            if (res.data.value === 200) {
                this.pageData.pagination.total = res.data.data.total
                this.pageData.table.data.length = 0

                res.data.data.records.forEach((item: any) => {
                    item.reportStatus = item.reportInfoDTO ? item.reportInfoDTO.reportStatus : ''
                    this.pageData.table.data.push(item)
                })
            }
        })
    }
    onSearch = (arg?: any) => {
        this.companyId = arg.companyId
        this.productType = arg.productType
        this.getData()
    }
    onReset = (arg?: any) => {
        this.pageData.pagination.pageNo = 1
        this.date.length = 0
        this.companyId = ''
        this.productType = 0
        this.getData()
    }
    onPaginationChange = (arg?: any) => {
        this.pageData.pagination.pageNo = arg
        this.getData()
    }
    onPaginationSize = (arg?: any) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = arg
        this.getData()
    }
    onOperation = (arg?: any) => {
        const { type, data } = arg
        if (type === 'export') {
            let params = ''
            if (this.uStore.isSuperAdmin || Number(this.uStore.companyId) === 1) {
                params = `current=99&size=99&companyId=${this.companyId}&productType=${
                    Number(this.productType) ? this.productType : ''
                }&queryDateStart=${this.date[0]}&queryDateEnd=${this.date[1]}`
            } else {
                params = `current=99&size=99&companyId=${this.uStore.companyId}&productType=${
                    Number(this.productType) ? this.productType : ''
                }&queryDateStart=${this.date[0]}&queryDateEnd=${this.date[1]}`
            }
            console.log('params', params)
            const url = import.meta.env.VITE_HOST + '/api/mall/company/store/report-order-list/export?' + params

            CreateXMLHttpRequest(url, '消费列表')
        }
    }
}
